﻿using CMS.DAL;
using CMS.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CMS.BLL
{
    public class BizComment
    {
        public static bool Add(string idPost, string content, string idUser)
        {
            using (var db = new DBContext())
            {
                Comment comment = new Comment
                {
                    ID = Guid.NewGuid().ToString(),
                    PostID = idPost,
                    UserID = idUser,
                    Content = content,
                    DateCreate = DateTime.Now
                };
                db.Comments.Add(comment);
                db.SaveChanges();
                return true;
            }
        }
        public static bool Update(Comment _comment)
        {
            using (var db = new DBContext())
            {
                var comment = (from e in db.Comments
                               where e.ID == _comment.ID
                               select e).SingleOrDefault();
                if (comment == null)
                    return false;
                comment.PostID = _comment.PostID;
                comment.Content = _comment.Content;
                comment.UserID = _comment.UserID;
                db.SaveChanges();
                return true;
            }
        }
        public static bool Delete(string id)
        {
            using (var db = new DBContext())
            {
                var comment = (from e in db.Comments
                               where e.ID == id
                               select e).SingleOrDefault();
                if (comment == null)
                    return false;
                db.Comments.Remove(comment);
                db.SaveChanges();
                return true;
            }
        }
        public static Comment Get(string id)
        {
            using (var db = new DBContext())
            {
                return (from e in db.Comments where e.ID == id select e).SingleOrDefault();
            }
        }
        public static List<Comment> GetList(string idPost)
        {
            using (var db = new DBContext())
            {
                return (from e in db.Comments where e.PostID == idPost select e).ToList();
            }
        }
        public static List<Comment> GetByUser(string idUser)
        {
            using (var db = new DBContext())
            {
                return (from e in db.Comments where e.UserID == idUser select e).ToList();
            }
        }
    }
}